home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xedit / commands.c < prev    next >
C/C++ Source or Header  |  1994-09-27  |  7KB  |  263 lines

  1. /* $XConsortium: commands.c,v 1.32 91/07/09 15:50:59 rws Exp $ */
  2.  
  3. /*
  4.  *              COPYRIGHT 1987
  5.  *           DIGITAL EQUIPMENT CORPORATION
  6.  *               MAYNARD, MASSACHUSETTS
  7.  *            ALL RIGHTS RESERVED.
  8.  *
  9.  * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
  10.  * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
  11.  * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
  12.  * ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
  13.  *
  14.  * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT RIGHTS,
  15.  * APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN ADDITION TO THAT
  16.  * SET FORTH ABOVE.
  17.  *
  18.  *
  19.  * Permission to use, copy, modify, and distribute this software and its
  20.  * documentation for any purpose and without fee is hereby granted, provided
  21.  * that the above copyright notice appear in all copies and that both that
  22.  * copyright notice and this permission notice appear in supporting
  23.  * documentation, and that the name of Digital Equipment Corporation not be 
  24.  * used in advertising or publicity pertaining to distribution of the software
  25.  * without specific, written prior permission.
  26.  */
  27.  
  28. #include "xedit.h"
  29. #ifdef CRAY
  30. #include <unistd.h>
  31. #endif
  32.  
  33. extern Widget textwindow, labelwindow, filenamewindow;
  34.  
  35. void ResetSourceChanged();
  36.  
  37. static void ResetDC();
  38.  
  39. static Boolean double_click = FALSE, source_changed = FALSE;
  40.  
  41. /*    Function Name: AddDoubleClickCallback(w)
  42.  *    Description: Adds a callback that will reset the double_click flag
  43.  *                   to false when the text is changed.
  44.  *    Arguments: w - widget to set callback upon.
  45.  *                 state - If true add the callback, else remove it.
  46.  *    Returns: none.
  47.  */
  48.  
  49. AddDoubleClickCallback(w, state)
  50. Widget w;
  51. Boolean state;
  52. {
  53.   Arg args[1];
  54.   static XtCallbackRec cb[] = { {NULL, NULL}, {NULL, NULL} };
  55.  
  56.   if (state) 
  57.     cb[0].callback = ResetDC;
  58.   else
  59.     cb[0].callback = NULL;
  60.  
  61.   XtSetArg(args[0], XtNcallback, cb);
  62.   XtSetValues(w, args, ONE);
  63. }
  64.   
  65. /*    Function Name: ResetDC
  66.  *    Description: Resets the double click flag.
  67.  *    Arguments: w - the text widget.
  68.  *                 junk, garbage - *** NOT USED ***
  69.  *    Returns: none.
  70.  */
  71.  
  72. /* ARGSUSED */
  73. static void
  74. ResetDC(w, junk, garbage)
  75. Widget w;
  76. caddr_t junk, garbage;
  77. {
  78.   double_click = FALSE;
  79.  
  80.   AddDoubleClickCallback(w, FALSE);
  81. }
  82.  
  83. void
  84. DoQuit()
  85. {
  86.   if( double_click || !source_changed ) 
  87.     exit(0); 
  88.  
  89.   XeditPrintf("Unsaved changes. Save them, or Quit again.\n");
  90.   Feep();
  91.   double_click = TRUE;
  92.   AddDoubleClickCallback(textwindow, TRUE);
  93. }
  94.  
  95. char *
  96. makeBackupName(buf, filename)
  97. String buf, filename;
  98. {
  99.   sprintf(buf, "%s%s%s", app_resources.backupNamePrefix,
  100.       filename, app_resources.backupNameSuffix);
  101.   return (buf);
  102. }
  103.   
  104. #if defined(USG) && !defined(CRAY)
  105. int rename (from, to)
  106.     char *from, *to;
  107. {
  108.     (void) unlink (to);
  109.     if (link (from, to) == 0) {
  110.         unlink (from);
  111.         return 0;
  112.     } else {
  113.         return -1;
  114.     }
  115. }
  116. #endif
  117.  
  118. void
  119. DoSave()
  120. {
  121.   String filename = GetString(filenamewindow);
  122.   char buf[BUFSIZ];
  123.  
  124.   if( (filename == NULL) || (strlen(filename) == 0) ){
  125.     XeditPrintf("Save:  no filename specified -- nothing saved\n");
  126.     Feep();
  127.     return;
  128.   }
  129.   
  130.   if (app_resources.enableBackups) {
  131.     char backup_file[BUFSIZ];
  132.     makeBackupName(backup_file, filename);
  133.  
  134.     if (rename(filename, backup_file) != 0) {
  135.       sprintf(buf, "error backing up file:  %s\n",  backup_file); 
  136.       XeditPrintf(buf);
  137.     }
  138.   }
  139.   
  140.   switch( MaybeCreateFile(filename)) {
  141.   case NO_READ:
  142.   case READ_OK:
  143.       sprintf(buf, "File %s could not be opened for writing.\n", filename);
  144.       break;
  145.   case WRITE_OK:
  146.       if ( XawAsciiSaveAsFile(XawTextGetSource(textwindow), filename) ) {
  147.       sprintf(buf, "Saved file:  %s\n", filename);
  148.       ResetSourceChanged(textwindow);
  149.       }
  150.       else 
  151.       sprintf(buf, "Error saving file:  %s\n",  filename);
  152.       break;
  153.   default:
  154.       sprintf(buf, "%s %s", "Internal function MaybeCreateFile()",
  155.           "returned unexpected value.\n");
  156.   }
  157.  
  158.   XeditPrintf(buf);
  159. }
  160.  
  161. void
  162. DoLoad()
  163. {
  164.     Arg args[5];
  165.     Cardinal num_args = 0;
  166.     String filename = GetString(filenamewindow);
  167.     char buf[BUFSIZ], label_buf[BUFSIZ];
  168.  
  169.     if ( source_changed && !double_click) {
  170.     XeditPrintf("Unsaved changes. Save them, or press Load again.\n");
  171.     Feep();
  172.     double_click = TRUE;
  173.     AddDoubleClickCallback(textwindow, TRUE);
  174.     return;
  175.     }
  176.     double_click = FALSE;
  177.     
  178.     if ( (filename != NULL)  &&  ((int) strlen(filename) > 0) ) {
  179.     Boolean exists;
  180.  
  181.     switch( CheckFilePermissions(filename, &exists) ) {
  182.     case NO_READ:
  183.         if (exists)
  184.         sprintf(buf, "File %s, %s", filename,
  185.             "exists, and could not be opened for reading.\n");
  186.         else
  187.         sprintf(buf, "File %s %s %s",  filename, "does not exist, and",
  188.             "the directory could not be opened for writing.\n");
  189.  
  190.         XeditPrintf(buf);
  191.         Feep();
  192.         return;
  193.     case READ_OK:
  194.         XtSetArg(args[num_args], XtNeditType, XawtextRead); num_args++;
  195.         sprintf(label_buf, "%s       READ ONLY", filename);
  196.         sprintf(buf, "File %s opened READ ONLY.\n", filename);
  197.         break;
  198.     case WRITE_OK:
  199.         XtSetArg(args[num_args], XtNeditType, XawtextEdit); num_args++;
  200.         sprintf(label_buf, "%s       Read - Write", filename);
  201.         sprintf(buf, "File %s opened read - write.\n", filename);
  202.         break;
  203.     default:
  204.         sprintf(buf, "%s %s", "Internal function MaybeCreateFile()",
  205.             "returned unexpected value.\n");
  206.         XeditPrintf(buf);
  207.         return;
  208.     }
  209.  
  210.     XeditPrintf(buf);
  211.     
  212.     if (exists) {
  213.         XtSetArg(args[num_args], XtNstring, filename); num_args++;
  214.     }
  215.     else {
  216.         XtSetArg(args[num_args], XtNstring, NULL); num_args++;
  217.     }
  218.  
  219.     XtSetValues( textwindow, args, num_args);
  220.     
  221.     num_args = 0;
  222.     XtSetArg(args[num_args], XtNlabel, label_buf); num_args++;
  223.     XtSetValues( labelwindow, args, num_args);
  224.     ResetSourceChanged(textwindow);
  225.     return;
  226.     }
  227.  
  228.     XeditPrintf("Load: No file specified.\n");
  229.     Feep();
  230. }
  231.  
  232. /*    Function Name: SourceChanged
  233.  *    Description: A callback routine called when the source has changed.
  234.  *    Arguments: w - the text source that has changed.
  235.  *                 junk, garbage - *** UNUSED ***.
  236.  *    Returns: none.
  237.  */
  238.  
  239. static void
  240. SourceChanged(w, junk, garbage)
  241. Widget w;
  242. XtPointer junk, garbage;
  243. {
  244.     XtRemoveCallback(w, XtNcallback, SourceChanged, NULL);
  245.     source_changed = TRUE;
  246. }
  247.  
  248. /*    Function Name: ResetSourceChanged.
  249.  *    Description: Sets the source changed to FALSE, and
  250.  *                   registers a callback to set it to TRUE when
  251.  *                   the source has changed.
  252.  *    Arguments: widget - widget to register the callback on.
  253.  *    Returns: none.
  254.  */
  255.  
  256. void
  257. ResetSourceChanged(widget)
  258. Widget widget;
  259. {
  260.     XtAddCallback(XawTextGetSource(widget), XtNcallback, SourceChanged, NULL);
  261.     source_changed = FALSE;
  262. }
  263.